// packageA/mycomponent/jianzhimaoPopup/jianzhimaoPopup.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    set_verify: {
      type: null
    },
    bank_card_status: {
      type: Boolean,
      value: false
    },
    payType:{
      type:String,
      default:""
    }
  },
  observers: {
    'set_verify': function (nval) {
      if (nval.length > 0) {
        nval.forEach((ele, ind) => {
          if (ele.value) {
            this.data.form[ele.name] = ele.value;
            this.setData({
              form: this.data.form
            })
            console.log(this.data.form,'ddddddddddddd')
          }
        });
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    form: {},
    bankdata: {
      bankname: '',
      Province: '',
      city: '',
      branchName: '',
      card: '',
      name: ''
    },

    provinceData: [],
    cityData: [],
    showCityData: [],
    ProvinceId: '',
    addressShow_p: false,
    addressShow_c: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    tapinpbank(e) {
      let key = e.currentTarget.dataset.key;
      let value = e.detail;
      this.setData({
        ['bankdata.' + key]: value
      });
    },
    tapinpForm(e) {
      let key = e.currentTarget.dataset.key;
      let value = e.detail;
      this.setData({
        ['form.' + key]: value
      });
    },
    show1CloseBtn() {
      this.triggerEvent('closePopup')
    },
    bindCard() {
      let json = {};
      if (this.data.bank_card_status) {
        json.bank = {};
        json.bank.bank_card = this.data.bankdata.bank_card;
        json.bank.bank_name = this.data.bankdata.bank_name;
        json.bank.bank_province = this.data.bankdata.bank_province;
        json.bank.bank_city = this.data.bankdata.bank_city;
        json.bank.bank_branch = this.data.bankdata.bank_branch;
        json.bank.type = 1;
        if (!this.data.bankdata.bank_name) {
          app.tips('请输入开户行')
          return
        } else if (!this.data.bankdata.bank_province) {
          app.tips('请选择所在省')
          return
        } else if (!this.data.bankdata.bank_city) {
          app.tips('请选择所在市')
          return
        } else if (!this.data.bankdata.bank_branch) {
          app.tips('请输入支行名称')
          return
        } else if (!this.data.bankdata.bank_card) {
          app.tips('请输入卡号')
          return
        }
      }

      for (let i = 0; i < this.data.set_verify.length; i++) {
        if (!this.data.set_verify[i].status) {
          json[this.data.set_verify[i].name] = this.data.form[this.data.set_verify[i].name];
          if (!this.data.form[this.data.set_verify[i].name]) {
            app.tips(`请输入${this.data.set_verify[i].tips}`)
            return;
          }
        }
      }
      let urlStr = app.getNetAddresss("plugin.jianzhimao-withdraw.frontend.member-info.set");
      if(this.data.payType == "huiis_ali" || this.data.payType == "huiis_wx" || this.data.payType == "huiis_bank" || this.data.payType == "cloud_pay_money_sign"){
        //云汇算提现到支付宝，微信，银行卡
        urlStr = app.getNetAddresss("plugin.cloud-pay-money.frontend.controllers.practitioner.practitioner.save");
        json.member_info = {...this.data.form}
      }
      app._postNetWork({
        url: urlStr,
        data: json,
        success: ((resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            if(this.data.payType == "huiis_ali" || this.data.payType == "huiis_wx" || this.data.payType == "huiis_bank"){
              this.show1CloseBtn();
              wx.navigateTo({
                url: '/mircoApp/cloud-pay-money/cloud_pay_money_sign',
              })
              return
            }
            this.show1CloseBtn();
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }

        }),
      });
    },
    addressCallback(e) {
      console.log(e);
      this.setData({
        'bankdata.bank_province': e.detail.value.areaname,
        ProvinceId: e.detail.value.id,
        showCityData: [],
        'bankdata.bank_city': '',
      });
      this.onCancel();
    },
    addressCallback_c(e) {
      this.setData({
        'bankdata.bank_city': e.detail.value.areaname,
      });
      this.onCancel();
    },
    onCancel() {
      this.setData({
        addressShow_p: false,
        addressShow_c: false
      });
    },
    // 选择市
    addressShowCity() {
      if (!this.data.ProvinceId) {
        wx.showToast({
          title: '请先选择省',
          icon: 'none',
          duration: 1000
        });
        return;
      }
      for (let i = 0; i < this.data.cityData.length; i++) {
        if (this.data.cityData[i].parentid == this.data.ProvinceId) {
          this.setData({
            showCityData: this.data.showCityData.concat(this.data.cityData[i]),
            addressShow_c: true
          });
        }
      }
    },
    // 选择省
    addressShowProvince() {
      if (this.data.provinceData.length > 0) {
        this.setData({
          createCardShow: false,
          addressShow_p: true
        });
      } else {
        let urlStr = app.getNetAddresss("member.member-address.address");
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: this.data.form,
          success: ((resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              this.setData({
                provinceData: res.data.province,
                cityData: res.data.city,
                addressShow_p: true
              });
            } else {
              wx.showToast({
                icon: 'none',
                title: res.msg,
                duration: 1500
              });
            }
          }),
          fail: ((err) => {
            console.log(err);
          })
        });
      }
    },
  }
})